package Frame.sayoptional;

import java.util.HashMap;

import static FCRendering.FCRMaster.fcv;

public class SayOptionalHelper {
    static SayOptionalHelper sayOptionalHelper;

    static {
        sayOptionalHelper = new SayOptionalHelper();
    }

    private HashMap<String, ISayoption> sayoptionHashMap = new HashMap<>();

    public SayOptionalHelper() {
        sayoptionHashMap.put("say", new SayTypeSayOptional());
        sayoptionHashMap.put("open", new OpenTypeOptional());
        sayoptionHashMap.put("fight", new FightTypeOptional());
        sayoptionHashMap.put("joinOp", new NpcJoinTypeOptional());
        sayoptionHashMap.put("reborn", new RebornTypeOptional());
        sayoptionHashMap.put("player", new PlayerTypeOptional());
        sayoptionHashMap.put("sleep", new SleepTypeOptional());
    }

    public static SayOptionalHelper getIn() {
        return sayOptionalHelper;
    }

    /**
     * say open fight joinOp这些，不断添加
     */
    public void clickSayOption(SayTextAndOption sayTextAndOption) {
        ISayoption sayoption = sayoptionHashMap.get(sayTextAndOption.optionType);
        if (sayoption != null) {
            sayoption.clickWhichOption(sayTextAndOption);
        }
    }

    /**
     * 生成对话右侧的菜单选项menu
     */
    public void createSayMenu() {
        if (fcv.getSayText() != null && fcv.getSayNpcSelectMenu().size() < 1) {
            int i = 0;
            for (String qw : fcv.getSayText()) {
                System.out.println("createSayMenu---"+qw);
                if (qw.contains(" id='")) {
                    //例如 dongxue-1-2-ItemCtrl-1<option1 id='dongxue-1-2-ItemCtrl-21' eventId='dongxue-1-2-on'><option2 id='dongxue-1-2-ItemCtrl-2'>
                    SayTextAndOption op2 = new SayTextAndOption("say", fcv.getSayText()[0], i++, getFenGeString(qw,"id='"));
                    op2.setEventId(getFenGeString(qw, "eventId='"));
                    fcv.getSayNpcSelectMenu().add(op2);
                } else if (qw.contains(" open='")) {
                    SayTextAndOption op2 = new SayTextAndOption("open", fcv.getSayText()[0], i++, getFenGeString(qw,"id='"));
                    op2.setEventId(getFenGeString(qw, "open='"));
                    op2.setParam(getFenGeString(qw, "param='"));
                    fcv.getSayNpcSelectMenu().add(op2);
                } else if (qw.contains(" fight='")) {
                    String[] mm = qw.split(" fight='");
                    fcv.getSayNpcSelectMenu().add(new SayTextAndOption("fight", fcv.getSayText()[0], i++, mm[1].replaceAll("'>", "")));
                } else if (qw.contains(" joinOp='")) {
                    String[] mm = qw.split(" joinOp='");
                    SayTextAndOption op = new SayTextAndOption("joinOp", fcv.getSayText()[0], i++, mm[1].replaceAll("'>", ""));
                    fcv.getSayNpcSelectMenu().add(op);
                    op.setParam(getFenGeString(qw, "player='"));
                } else if (qw.contains(" reborn='")) {
                    String[] mm = qw.split(" reborn='");
                    fcv.getSayNpcSelectMenu().add(new SayTextAndOption("reborn", fcv.getSayText()[0], i++, mm[1].replaceAll("'>", "")));
                } else if (qw.contains(" sleep='")) {
                    //比如 1 sleep='xxxx' gold='5' eventId=''> 花费5金，之后跳转执行event（jump类型）
                    SayTextAndOption op = new SayTextAndOption("sleep", fcv.getSayText()[0], i++, getFenGeString(qw, "sleep='"));
                    String gold = getFenGeString(qw, "gold='");
                    op.setNeedMoney(gold == null ? 0 : Integer.parseInt(gold));
                    op.setEventId(getFenGeString(qw, "eventId='"));
                    fcv.getSayNpcSelectMenu().add(op);
                }
            }
        }
    }

    private String getFenGeString(String src, String key) {
        String[] t = src.split(key);
        try {
            if (t.length > 0) {
                return t[1].substring(0, t[1].indexOf("'"));
            }
        } catch (Exception e) {
        }
        return null;
    }
}
